package cn.lisens.demo.web;

import cn.lisens.demo.entity.User;
import cn.lisens.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * User控制器接口
 */
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 创建用户
     */
    @PostMapping
    public User createUser(@RequestBody User user) {
        return userService.createUser(user);
    }

    /**
     * 根据用户编码获取用户
     */
    @GetMapping("/{userCode}")
    public User getUserByCode(@PathVariable String userCode) {
        return userService.getUserByCode(userCode);
    }

    /**
     * 获取所有用户
     */
    @GetMapping
    public List<User> getAllUsers() {
        return userService.getAllUsers();
    }

    /**
     * 更新用户信息
     */
    @PostMapping("/update/{userCode}")
    public User updateUser(@PathVariable String userCode, @RequestBody User user) {
        return userService.updateUser(userCode, user);
    }

    /**
     * 删除用户
     */
    @GetMapping("/delete/{userCode}")
    public void deleteUser(@PathVariable String userCode) {
        userService.deleteUser(userCode);
    }
}
